package com.winit.config.repository;

import com.alibaba.fastjson.JSONObject;
import com.datastax.driver.core.ConsistencyLevel;
import com.datastax.driver.core.querybuilder.Insert;
import com.datastax.driver.core.querybuilder.QueryBuilder;
import com.datastax.driver.core.querybuilder.Select;
import com.datastax.driver.core.utils.UUIDs;
import com.winit.common.util.Constants;
import com.winit.config.model.Config;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.cassandra.core.CassandraOperations;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;
import java.util.List;
import java.util.UUID;

/**
 * Created by wangjian on 2017/9/28.
 */
@Component
public class ConfigOperations {

    private static Logger logger = LoggerFactory.getLogger(ConfigOperations.class);

    @Autowired
    private CassandraOperations cassandraOperations;

    public List<Config> getMasterConfig(String appTag) {
        Select select = QueryBuilder.select().from("uconfig_config");
        if (!StringUtils.isEmpty(appTag)) {
            select.where(QueryBuilder.eq("config_app_tag", appTag));
        }
        select.where(QueryBuilder.eq("config_type_tag", Constants.CONFIG_TYPE_APP));
        select.where(QueryBuilder.eq("config_version", Constants.CONFIG_VERSION_MASTER));
        select.allowFiltering();
        List<Config> list = cassandraOperations.select(select, Config.class);
        return list;
    }

    public List<Config> getResourceConfig(UUID id, String appTag, String envTag, String zoneTag, String resourceTag) {
        Select select = QueryBuilder.select().from("uconfig_config");
        if (!StringUtils.isEmpty(id)) {
            select.where(QueryBuilder.eq("config_id", id));
        }
        if (!StringUtils.isEmpty(appTag)) {
            select.where(QueryBuilder.eq("config_app_tag", appTag));
        }
        if (!StringUtils.isEmpty(envTag)) {
            select.where(QueryBuilder.eq("config_env_tag", envTag));
        }
        if (!StringUtils.isEmpty(zoneTag)) {
            select.where(QueryBuilder.eq("config_zone_tag", zoneTag));
        }
        if (!StringUtils.isEmpty(resourceTag)) {
            select.where(QueryBuilder.eq("config_resource_tag", resourceTag));
        }
        select.where(QueryBuilder.eq("config_type_tag", Constants.CONFIG_TYPE_RESOURCE));
        select.allowFiltering();
        List<Config> list = cassandraOperations.select(select, Config.class);
        return list;
    }

    public List<Config> getServerConfig(UUID id, String envTag, String zoneTag, String resourceTag) {
        Select select = QueryBuilder.select().from("uconfig_config");
        if (!StringUtils.isEmpty(id)) {
            select.where(QueryBuilder.eq("config_id", id));
        }
        if (!StringUtils.isEmpty(envTag)) {
            select.where(QueryBuilder.eq("config_env_tag", envTag));
        }
        if (!StringUtils.isEmpty(zoneTag)) {
            select.where(QueryBuilder.eq("config_zone_tag", zoneTag));
        }
        if (!StringUtils.isEmpty(resourceTag)) {
            select.where(QueryBuilder.eq("config_resource_tag", resourceTag));
        }
        select.where(QueryBuilder.eq("config_type_tag", Constants.CONFIG_TYPE_SERVER));
        select.allowFiltering();
        List<Config> list = cassandraOperations.select(select, Config.class);
        return list;
    }

    public void addConfig(String appTag, String envTag, JSONObject kvmap, JSONObject refid, JSONObject reftag
            , String resTag, String typeTag, String version, String zoneTag, String deplZone, String deplTag) {
        addConfig(appTag, envTag, kvmap, refid, reftag, resTag, typeTag, version, zoneTag, deplZone, deplTag, null);
    }

    public void addConfig(String appTag, String envTag, JSONObject kvmap, JSONObject refid, JSONObject reftag
            , String resTag, String typeTag, String version, String zoneTag, String deplZone, String deplTag, String desc) {
        Insert insert = QueryBuilder.insertInto("uconfig_config");
        insert.setConsistencyLevel(ConsistencyLevel.ONE);
        UUID uuid = UUIDs.timeBased();
        System.out.println(uuid);
        insert.value("config_id", uuid);
        if (!StringUtils.isEmpty(appTag)) {
            insert.value("config_app_tag", appTag);
        }
        if (!StringUtils.isEmpty(envTag)) {
            insert.value("config_env_tag", envTag);
        }
        if (null != kvmap) {
            insert.value("config_k_v_map", kvmap);
        }
        if (null != refid) {
            insert.value("config_k_v_ref_id", refid);
        }
        if (null != reftag) {
            insert.value("config_k_v_ref_tag", reftag);
        }
        if (!StringUtils.isEmpty(resTag)) {
            insert.value("config_resource_tag", resTag);
        }
        if (!StringUtils.isEmpty(typeTag)) {
            insert.value("config_type_tag", typeTag);
        }
        if (!StringUtils.isEmpty(version)) {
            insert.value("config_version", version);
        }
        if (!StringUtils.isEmpty(zoneTag)) {
            insert.value("config_zone_tag", zoneTag);
        }
        if (!StringUtils.isEmpty(deplZone)) {
            insert.value("config_deploy_zone_code", deplZone);
        }
        if (!StringUtils.isEmpty(deplTag)) {
            insert.value("config_deploy_tag", deplTag);
        }
        if (!StringUtils.isEmpty(desc)) {
            insert.value("config_desc", desc);
        }
        cassandraOperations.execute(insert);
    }
}
